home *** CD-ROM | disk | FTP | other *** search
/ Software of the Month Club 1996 April / Software of the Month Club 1996 April.iso / pc / os2 / psutils / src / extrac~1.pl < prev    next >
Text File  |  1996-02-21  |  3KB  |  97 lines

  1. @PERL@
  2. # extractres: extract resources from PostScript file
  3. #
  4. # Copyright (C) Angus J. C. Duggan 1991-1995
  5. # See file LICENSE for details.
  6.  
  7. $prog = ($0 =~ s=.*/==);
  8.  
  9. %resources = ();        # list of resources included
  10. %merge = ();            # list of resources extracted this time
  11. %extn = ("font", ".pfa", "file", ".ps", "procset", ".ps", # resource extns
  12.      "pattern", ".pat", "form", ".frm", "encoding", ".enc");
  13. %type = ("%%BeginFile:", "file", "%%BeginProcSet:", "procset",
  14.      "%%BeginFont:", "font"); # resource types
  15.  
  16. while (@ARGV) {
  17.    $_ = shift;
  18.    if (/^-m(erge)?$/) { $merge = 1; }
  19.    elsif (/^-/) {
  20.       print STDERR "Usage: $prog [-merge] [file]\n";
  21.       exit 1;
  22.    } else {
  23.       unshift(@ARGV, $_);
  24.       last;
  25.    }
  26. }
  27.  
  28. if (defined($ENV{TMPDIR})) {    # set body file name
  29.    $body = "$ENV{TMPDIR}/body$$.ps";
  30. } else {
  31.    $body = "body$$.ps";
  32. }
  33.  
  34. open(BODY, $body) && die "Temporary file $body already exists";
  35. open(BODY, ">$body") || die "Can't write file $body";
  36.  
  37. sub filename {            # make filename for resource in @_
  38.    local($name);
  39.    foreach (@_) {        # sanitise name
  40.       s/[!()\$\#*&\\\|\`\'\"\~\{\}\[\]\<\>\?]//g;
  41.       $name .= $_;
  42.    }
  43.    $name =~ s@.*/@@;        # drop directories
  44.    die "Filename not found for resource ", join(" ", @_), "\n"
  45.       if $name =~ /^$/;
  46.    $name;
  47. }
  48.  
  49. $output = STDOUT;        # start writing header out
  50. while (<>) {
  51.    if (/^%%BeginResource:/ || /^%%BeginFont:/ || /^%%BeginProcSet:/) {
  52.       local($comment, @res) = split(/\s+/); # look at resource type
  53.       local($type) = defined($type{$comment}) ? $type{$comment} : shift(@res);
  54.       local($name) = &filename(@res, $extn{$type}); # make file name
  55.       $saveout = $output;
  56.       if (!$resources{$name}) {
  57.      print "%%IncludeResource: $type ", join(" ", @res), "\n";
  58.      if (!open(RES, $name)) {
  59.         open(RES, ">$name") || die "Can't write file $name";
  60.         $resources{$name} = $name;
  61.         $merge{$name} = $merge;
  62.         $output = RES;
  63.      } else {        # resource already exists
  64.         close(RES);
  65.         undef $output;
  66.      }
  67.       } elsif ($merge{$name}) {
  68.      open(RES, ">>$name") || die "Can't append to file $name";
  69.      $output = RES;
  70.       } else {            # resource already included
  71.      undef $output;
  72.       }
  73.    } elsif (/^%%EndResource/ || /^%%EndFont/ || /^%%EndProcSet/) {
  74.       if (defined $output) {
  75.      print $output $_;
  76.      close($output);
  77.       }
  78.       $output = $saveout;
  79.       next;
  80.    } elsif ((/^%%EndProlog/ || /^%%BeginSetup/ || /^%%Page:/)) {
  81.       $output = BODY;
  82.    }
  83.    print $output $_
  84.       if defined $output;
  85. }
  86.  
  87. close(BODY);            # close body output file
  88.  
  89. open(BODY, $body);        # reopen body for input
  90. while (<BODY>) {        # print it all
  91.    print $_;
  92. }
  93. close(BODY);
  94.  
  95. unlink($body);            # dispose of body file
  96. @END@
  97.